/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.config.service.impl;

import com.icinfo.cloud.provider.punish.config.dto.CaseRecallOperateDto;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigBusiCaseRecallColumnMapper;
import com.icinfo.cloud.provider.punish.config.model.ConfigBusiCaseRecallColumn;
import com.icinfo.cloud.provider.punish.config.service.IConfigBusiCaseRecallColumnService;
import com.icinfo.cloud.provider.punish.config.vo.BusiTableRecallOperateVo;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述: 案件撤回相关操作配置表 config_busi_case_recall_column 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年08月28日
 */
@Slf4j
@Service
public class ConfigBusiCaseRecallColumnServiceImpl extends MyBatisServiceSupport implements IConfigBusiCaseRecallColumnService {

    @Resource
    private ConfigBusiCaseRecallColumnMapper configBusiCaseRecallColumnMapper;

    /**
     * 根据案件撤回配置更新相关数据
     *
     * @param operateDto
     * @return
     * @author zhengqiang
     * @date 2022/8/28
     */
    @Override
    public void operateBusiTableByRecallLinkCode(CaseRecallOperateDto operateDto) {
        //查询撤回环节配置操作表
        List<BusiTableRecallOperateVo> busiTableOperateVoList = configBusiCaseRecallColumnMapper.selectTableNameListByCaseLinkCode(operateDto.getCaseLinkCode());
        if (null != busiTableOperateVoList && busiTableOperateVoList.size() > 0) {
            for (BusiTableRecallOperateVo busiTableOperateVo : busiTableOperateVoList) {

                Example example = new Example(ConfigBusiCaseRecallColumn.class);
                example.createCriteria().andEqualTo("caseLinkCode", operateDto.getCaseLinkCode()).andEqualTo("busiTableNameEn", busiTableOperateVo.getBusiTableNameEn()).andEqualTo("busiOperateType", busiTableOperateVo.getBusiOperateType());
                List<ConfigBusiCaseRecallColumn> configBusiParamDocumentColumnList = configBusiCaseRecallColumnMapper.selectByExample(example);

                //配置删除操作
                if ("D".equals(busiTableOperateVo.getBusiOperateType())) {
                    deleteBusiTable(busiTableOperateVo, operateDto, configBusiParamDocumentColumnList);
                }

                if ("U".equals(busiTableOperateVo.getBusiOperateType())) {
                    updateBusiTable(busiTableOperateVo, operateDto, configBusiParamDocumentColumnList);
                }
                //TODO 下面这块暂时没时间改，后面补上
                /*if ("I,U".equals(busiTableOperateVo.getBusiOperateType())) {
                    //先判断是否存在
                    String selectSql = "select count(1) from upcase." + busiTableOperateVo.getBusiTableNameEn() + " where case_id='" + operateDto.getCaseId() + "'";
                    int num = configBusiCaseRecallColumnMapper.executeSelectNumSql(selectSql);
                    if (num > 0) {
                        //存在则更新
                        updateBusiTable(busiTableOperateVo, operateDto, configBusiParamDocumentColumnList);

                    } else {
                        //不存在则插入
                        Map<String, String> insertMap = new HashMap<>();
                        //组装map
                        for (ConfigBusiCaseRecallColumn configBusiParamDocumentColumn : configBusiParamDocumentColumnList) {
                            if ("id".equals(configBusiParamDocumentColumn.getBusiOperateCondition())) {
                                insertMap.put(configBusiParamDocumentColumn.getBusiColumnEn(), StringUtils.uuid());
                                continue;
                            }
                            if ("case_id".equals(configBusiParamDocumentColumn.getBusiOperateCondition())) {
                                insertMap.put(configBusiParamDocumentColumn.getBusiColumnEn(), operateDto.getCaseId());
                                continue;
                            }
                            if ("create_time".equals(configBusiParamDocumentColumn.getBusiOperateCondition())) {
                                insertMap.put(configBusiParamDocumentColumn.getBusiColumnEn(), DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
                                continue;
                            }

                            String columnValue = configBusiParamDocumentColumn.getBusiColumnValue();

                            insertMap.put(configBusiParamDocumentColumn.getBusiColumnEn(), columnValue);
                        }
                        //生成INSERT INTO table(field1,field2) 部分
                        StringBuffer sbField = new StringBuffer();
                        //生成VALUES('value1','value2') 部分
                        StringBuffer sbValue = new StringBuffer();
                        sbField.append("INSERT INTO  upcase." + busiTableOperateVo.getBusiTableNameEn() + "(");
                        for (Map.Entry<String, String> entry : insertMap.entrySet()) {
                            String mapKey = entry.getKey();
                            String mapValue = entry.getValue();
                            sbField.append(mapKey + ",");
                            sbValue.append("'" + mapValue + "',");
                        }
                        sbField = sbField.deleteCharAt(sbField.length() - 1);
                        sbValue = sbValue.deleteCharAt(sbValue.length() - 1);
                        String insertSql = sbField.toString() + ") VALUES(" + sbValue.toString() + ")";
                        configBusiCaseRecallColumnMapper.executeInsertSql(insertSql);
                    }


                }*/
            }
        }
    }

    /**
     * 删除操作
     *
     * @param busiTableOperateVo
     * @param operateDto
     * @return
     * @author zhengqiang
     * @date 2022/8/31
     */
    private void deleteBusiTable(BusiTableRecallOperateVo busiTableOperateVo, CaseRecallOperateDto operateDto, List<ConfigBusiCaseRecallColumn> configBusiCaseRecallColumnList) {
        String tableName = busiTableOperateVo.getBusiTableNameEn();
        String caseId = operateDto.getCaseId();
        //String operateCondition = "";
        boolean isHasOperateCondition = false;
        for (ConfigBusiCaseRecallColumn configBusiCaseRecallColumn : configBusiCaseRecallColumnList) {
            //BusiOperateCondition 不为空 此字段不更新
            if("2".equals(configBusiCaseRecallColumn.getConfigColumnType()) && StringUtils.isNotBlank(configBusiCaseRecallColumn.getBusiColumnEn())){
                if ("case_id".equals(configBusiCaseRecallColumn.getBusiColumnEn())) {
                    isHasOperateCondition = true;
                    //operateCondition += " and case_id = '" + caseId + "' ";
                }
                continue;
            }
        }
        if (!isHasOperateCondition) {
            return;
        }
        String updateSql = "delete from upcase." + tableName + " where case_id = '" + caseId +"'";
        configBusiCaseRecallColumnMapper.executeUpdateSql(updateSql);
    }

    /**
     * 更新操作
     *
     * @param busiTableOperateVo
     * @param operateDto
     * @param configBusiCaseRecallColumnList
     * @return
     * @author zhengqiang
     * @date 2022/8/31
     */
    private void updateBusiTable(BusiTableRecallOperateVo busiTableOperateVo, CaseRecallOperateDto operateDto, List<ConfigBusiCaseRecallColumn> configBusiCaseRecallColumnList) {
        String tableName = busiTableOperateVo.getBusiTableNameEn();
        String caseId = operateDto.getCaseId();
        String setSql = "";
        String operateCondition = "";
        for (ConfigBusiCaseRecallColumn configBusiCaseRecallColumn : configBusiCaseRecallColumnList) {
            String columnValue = configBusiCaseRecallColumn.getBusiColumnValue();
            //拼装更新条件
            if("1".equals(configBusiCaseRecallColumn.getConfigColumnType())  && StringUtils.isNotBlank(configBusiCaseRecallColumn.getBusiColumnEn())){
                if (StringUtils.isBlank(setSql)) {
                    if (ObjectUtils.isEmpty(columnValue)) {
                        setSql = configBusiCaseRecallColumn.getBusiColumnEn() + "= null ";
                    } else {
                        columnValue= columnValue.replace("'","''");
                        setSql = configBusiCaseRecallColumn.getBusiColumnEn() + "='" + columnValue + "'";
                    }
                } else {
                    if (ObjectUtils.isEmpty(columnValue)) {
                        setSql = setSql + "," + configBusiCaseRecallColumn.getBusiColumnEn() + "=null ";
                    } else {
                        columnValue= columnValue.replace("'","''");
                        setSql = setSql + "," + configBusiCaseRecallColumn.getBusiColumnEn() + "='" + columnValue + "'";
                    }
                }
            }

            //拼装查询条件
            if("2".equals(configBusiCaseRecallColumn.getConfigColumnType()) && StringUtils.isNotBlank(configBusiCaseRecallColumn.getBusiColumnEn())){
                if ("case_id".equals(configBusiCaseRecallColumn.getBusiColumnEn())) {
                    if("case_main_info".equals(tableName)){
                        operateCondition += " and  id = '" + caseId + "' ";
                    }else {
                        operateCondition += " and case_id = '" + caseId + "' ";
                    }
                }
                continue;
            }
        }
        if (ObjectUtils.isEmpty(operateCondition)) {
            return;
        }
        String updateSql = "update upcase." + tableName + " set " + setSql + " where 1=1 " + operateCondition;

        configBusiCaseRecallColumnMapper.executeUpdateSql(updateSql);
    }
}
